//
//  XMPicwordPublishView.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/7/2.
//

#import "XMPicwordPublishView.h"
#import "XMTopicView.h"
#import "XMImgModel.h"

@interface XMPicwordPublishView () <UITextFieldDelegate, UITextViewDelegate>

@property (nonatomic, strong) UIScrollView *bodyScrollView;
@property (nonatomic, strong) UITextField *titleTextField;
@property (nonatomic, strong) UITextView *contentTextView;
@property (nonatomic, strong) UILabel *placeholderLabel;
@property (nonatomic, strong) UIButton *addImgBtn;
@property (nonatomic, strong) UIScrollView *imgBgView;
@property (nonatomic, strong) UIScrollView *topicBgView;

@property (nonatomic, strong) XMTopicView *topicView;

@property (nonatomic, strong) UILabel *addressLabel;
@property (nonatomic, strong) UISwitch *secretSwitch;

@property (nonatomic, strong) NSMutableArray *selectImgArr;
@property (nonatomic, strong) NSMutableArray *selectTopicArr;

@end

@implementation XMPicwordPublishView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    self.navTitleLabel.text = @"发布图文";
    [self addSubview:self.bodyScrollView];
    _bodyScrollView.backgroundColor = kColorHEX(0xF7F7F7);
    
    UIView *topContentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(420.0))];
    topContentView.backgroundColor = kColorHEX(0xffffff);
    [_bodyScrollView addSubview:topContentView];
    {
        [topContentView addSubview:self.titleTextField];
        [topContentView addSubview:self.contentTextView];
        [_contentTextView addSubview:self.placeholderLabel];
        [topContentView addSubview:self.imgBgView];
        [_imgBgView addSubview:self.addImgBtn];
        
        //话题
        UIButton *topicBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(12.0) frame:CGRectMake(KScaleWidth(18.0), _imgBgView.maxY + KScaleWidth(16.0), KScaleWidth(54.0), KScaleWidth(27.0)) title:@"#话题" titleColor:kColorHEX(0x333333)];
        topicBtn.backgroundColor = kColorHEX(0xF5F5F5);
        [topicBtn setCornerRadius:topicBtn.height/2];
        [topicBtn addTarget:self action:@selector(topicAddAction:) forControlEvents:UIControlEventTouchUpInside];
        [topContentView addSubview:topicBtn];
        [topContentView addSubview:self.topicBgView];
    }
    //位置
    UIView *positionBgView = [[UIView alloc] initWithFrame:CGRectMake(0, topContentView.maxY + KScaleWidth(10.0), self.width, KScaleWidth(58.0))];
    positionBgView.backgroundColor = kColorHEX(0xffffff);
    [_bodyScrollView addSubview:positionBgView];
    {
        UIImageView *posiIcon = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(26.0), 0, KScaleWidth(14.0), positionBgView.height)];
        posiIcon.contentMode = UIViewContentModeScaleAspectFit;
        posiIcon.image = ImageNamed(@"mine_address_icon");
        [positionBgView addSubview:posiIcon];
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(posiIcon.maxX + KScaleWidth(6.0), posiIcon.y, KScaleWidth(40.0), posiIcon.height) font:KFontSize(15.0) textColor:kColorHEX(0x9C9C9C)];
        titleLabel.text = @"位置";
        [positionBgView addSubview:titleLabel];
        UIImageView *nextIcon = [[UIImageView alloc] initWithFrame:CGRectMake(positionBgView.width - KScaleWidth(20.0) - KScaleWidth(8.0), 0, KScaleWidth(8.0), positionBgView.height)];
        nextIcon.contentMode = UIViewContentModeScaleAspectFit;
        nextIcon.image = ImageNamed(@"mine_detail_icon");
        [positionBgView addSubview:nextIcon];
        [positionBgView addSubview:self.addressLabel];
        UIButton *positionSelectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        positionSelectBtn.frame = positionBgView.bounds;
        [positionSelectBtn addTarget:self action:@selector(posiSelectAction:) forControlEvents:UIControlEventTouchUpInside];
        [positionBgView addSubview:positionSelectBtn];
    }
    //是否私密
    UIView *secrectView = [[UIView alloc] initWithFrame:CGRectMake(0, positionBgView.maxY + KScaleWidth(10.0), self.width, KScaleWidth(58.0))];
    secrectView.backgroundColor = kColorHEX(0xffffff);
    [_bodyScrollView addSubview:secrectView];
    {
        UIImageView *secrectIcon = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(26.0), 0, KScaleWidth(14.0), secrectView.height)];
        secrectIcon.contentMode = UIViewContentModeScaleAspectFit;
        secrectIcon.image = ImageNamed(@"");
        [secrectView addSubview:secrectIcon];
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(secrectIcon.maxX + KScaleWidth(6.0), secrectIcon.y, KScaleWidth(80.0)   , secrectIcon.height) font:KFontSize(15.0) textColor:kColorHEX(0x9C9C9C)];
        titleLabel.text = @"公开可见";
        [secrectView addSubview:titleLabel];
        [secrectView addSubview:self.secretSwitch];
        _secretSwitch.on = YES;
    }
    
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height - KScaleWidth(78.0) - kBottomLayout, self.width, KScaleWidth(78.0) + kBottomLayout)];
    bottomView.backgroundColor = kColorHEX(0xffffff);
    [self addSubview:bottomView];
    {
//        UIButton *draftBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(14.0) frame:CGRectMake(0, 0, KScaleWidth(84.0), KScaleWidth(78.0)) title:@"存草稿" titleColor:kColorHEX(0x121111) image:@""];
//        [draftBtn addTarget:self action:@selector(draftSaveAction:) forControlEvents:UIControlEventTouchUpInside];
//        [bottomView addSubview:draftBtn];
        UIButton *publishBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KBoldFontSize(17.0) frame:CGRectMake((bottomView.width - KScaleWidth(270.0))/2, (KScaleWidth(78.0) - KScaleWidth(48.0))/2, KScaleWidth(270.0), KScaleWidth(48.0)) title:@"发布" titleColor:kColorHEX(0x333333)];
        [publishBtn setGradientLayerWithLocations:@[@(0), @(1.0)] colors:@[(__bridge id)kColorHEX(0xFFD800).CGColor, (__bridge id)kColorHEX(0xFFE44E).CGColor] startPoint:CGPointMake(0.5, 0.37) endPoint:CGPointMake(0.03, 0.37)];
        [publishBtn setCornerRadius:publishBtn.height/2];
        [publishBtn addTarget:self action:@selector(publishAction:) forControlEvents:UIControlEventTouchUpInside];
        [bottomView addSubview:publishBtn];
    }
}

//图片上传成功
- (void)setSelectImgWithImgArr:(NSArray *)selectImgStrArr {
    [self.selectImgArr addObjectsFromArray:selectImgStrArr];
    [self setSelectImgInfo];
}

//话题
- (void)setTopicInfo {
    for (UIView *subView in self.topicBgView.subviews) {
        [subView removeFromSuperview];
    }
    CGFloat frameX = KScaleWidth(16.0);
    for (int i = 0; i < self.selectTopicArr.count; i ++) {
        NSString *title = self.selectTopicArr[i];
        CGFloat width = [title getWidthWithFont:KScaleWidth(12.0)] + KScaleWidth(20.0);
        UIButton *topicBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(12.0) frame:CGRectMake(frameX, (_topicBgView.height - KScaleWidth(27.0))/2, width, KScaleWidth(27.0)) title:self.selectTopicArr[i] titleColor:kColorHEX(0x9B9B9B)];
        topicBtn.backgroundColor = kColorHEX(0xF7F7F7);
        [topicBtn setCornerRadius:topicBtn.height/2];
        [topicBtn addTarget:self action:@selector(topicEditAction:) forControlEvents:UIControlEventTouchUpInside];
        [_topicBgView addSubview:topicBtn];
        frameX += (width + KScaleWidth(8.0));
    }
    _topicBgView.contentSize = CGSizeMake(MAX(frameX, _topicBgView.width), _topicBgView.height);
}

//图片
- (void)setSelectImgInfo {
    for (UIView *subView in self.imgBgView.subviews) {
        [subView removeFromSuperview];
    }
    [_imgBgView addSubview:self.addImgBtn];
    CGFloat frameX = KScaleWidth(15.0);
    for (int i = 0; i < self.selectImgArr.count; i ++) {
        XMImgModel *imgModel = self.selectImgArr[i];
        NSString *imgStr = imgModel.url;
        UIImageView *selectImg = [[UIImageView alloc] initWithFrame:CGRectMake(frameX , 0, KScaleWidth(90.0), KScaleWidth(78.0))];
        [selectImg setCornerRadius:KScaleWidth(10.0)];
        selectImg.userInteractionEnabled = YES;
        [selectImg sd_setImageWithURL:[NSURL URLWithString:imgStr]];
//        selectImg.image = _selectImgArr[i];
        [self.imgBgView addSubview:selectImg];
        
        UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(selectImg.width - KScaleWidth(20.0), 0, KScaleWidth(20.0), KScaleWidth(17.0)) image:@"pub_delete_icon" selectImg:@"pub_delete_icon"];
        [deleteBtn setCornerRadius:KScaleWidth(10.0)];
        [deleteBtn addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
        deleteBtn.tag = 11000 + i;
        [selectImg addSubview:deleteBtn];
        frameX += (KScaleWidth(90.0) + KScaleWidth(8.0));
    }
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.addImgBtn.x = frameX;
    }];
//    weakSelf.addImgBtn.x = frameX;
    frameX = frameX + _addImgBtn.width + KScaleWidth(8.0) + KScaleWidth(15.0);
    _imgBgView.contentSize = CGSizeMake(MAX(frameX, _imgBgView.width), _imgBgView.height);
}

- (void)setPositionSelectInfo:(NSString *)address {
    _addressLabel.text = address;
}

#pragma mark - BtnAction
//添加话题
- (void)topicAddAction:(UIButton *)button {
    [self addSubview:self.topicView];
    [_topicView showWithTopic:@""];
    XMWeakSelf;
    _topicView.topicSelectBlock = ^(NSString * _Nonnull topicStr) {
        if (topicStr.length > 0 && ![topicStr isEqualToString:@"#"]) {
            [weakSelf.selectTopicArr addObject:topicStr];
            [weakSelf setTopicInfo];
        }
    };
}

//话题编辑
- (void)topicEditAction:(UIButton *)button {
    [self addSubview:self.topicView];
    [self.topicView showWithTopic:button.titleLabel.text];
    XMWeakSelf;
    _topicView.topicSelectBlock = ^(NSString * _Nonnull topicStr) {
        if (topicStr.length > 0 || [topicStr isEqualToString:@"#"]) {
            [weakSelf.selectTopicArr removeObject:button.titleLabel.text];
            [weakSelf.selectTopicArr addObject:topicStr];
            [button setTitle:topicStr forState:UIControlStateNormal];
        }
        else {
            [weakSelf.selectTopicArr removeObject:button.titleLabel.text];
            [weakSelf.selectTopicArr removeObject:@""];
            [button removeFromSuperview];
            [weakSelf setTopicInfo];
        }
    };
}

//删除图片
- (void)deleteAction:(UIButton *)button {
    [self.selectImgArr removeObjectAtIndex:button.tag - 11000];
    [self setSelectImgInfo];
}

//添加图片
- (void)addImgAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(upLoadImg)]) {
        [self.delegate upLoadImg];
    }
}

//位置选择
- (void)posiSelectAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(positionSelectAction)]) {
        [self.delegate positionSelectAction];
    }
}

//存草稿
- (void)draftSaveAction:(UIButton *)button {
    
}

//发布
- (void)publishAction:(UIButton *)button {
    [self saveWithSaveType:@""];
}

- (void)saveWithSaveType:(NSString *)saveType {
    if (_titleTextField.text.length == 0) {
        [MBProgressHUD showError:@"请输入标题"];
        return;
    }
    if (_contentTextView.text.length == 0) {
        [MBProgressHUD showError:@"请输入内容"];
        return;
    }
    if (_selectImgArr.count == 0) {
        [MBProgressHUD showError:@"请添加图片"];
        return;
    }
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters safe_setObject:@"2" forKey:@"utype"];
    [parameters safe_setObject:_titleTextField.text forKey:@"title"];
    [parameters safe_setObject:_contentTextView.text forKey:@"content"];
    [parameters safe_setObject:[self.selectTopicArr componentsJoinedByString:@","] forKey:@"topics"];
    [parameters safe_setObject:_secretSwitch.isOn ? @"0" : @"1" forKey:@"isPrivate"];
    
    NSString *imgUrlStr = @"";
    for (XMImgModel *imgModel in self.selectImgArr) {
        if (imgUrlStr.length == 0) {
            imgUrlStr = imgModel.url;
            [parameters safe_setObject:imgModel.url forKey:@"coverImageUrl"];
        }
        else {
            
            imgUrlStr = [NSString stringWithFormat:@"%@,%@", imgUrlStr, imgModel.url];
        }
    }
    [parameters safe_setObject:imgUrlStr forKey:@"imgs"];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(publishWithParameters:)]) {
        [self.delegate publishWithParameters:parameters];
    }
}

#pragma mark - Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self endEditing:YES];
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView {
    self.placeholderLabel.hidden = !(textView.text.length == 0);
}

#pragma mark - Init
- (UIScrollView *)bodyScrollView {
    if (!_bodyScrollView) {
        _bodyScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, kNavagationBarH, self.width, self.height - kNavagationBarH - KScaleWidth(78.0) - kBottomLayout)];
    }
    return _bodyScrollView;
}

- (UITextField *)titleTextField {
    if (!_titleTextField) {
        _titleTextField = [[UITextField alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), KScaleWidth(20.0), self.width - KScaleWidth(16.0)*2, KScaleWidth(32.0))];
        _titleTextField.font = KFontSize(16.0);
        _titleTextField.textColor = kColorHEX(0x333333);
        _titleTextField.placeholder = @"填写标题会被更多人看到...";
        _titleTextField.returnKeyType = UIReturnKeyDone;
        _titleTextField.delegate = self;
    }
    return _titleTextField;
}

- (UITextView *)contentTextView {
    if (!_contentTextView) {
        _contentTextView = [[UITextView alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), self.titleTextField.maxY, self.width - KScaleWidth(16.0)*2, KScaleWidth(90.0))];
        _contentTextView.font = KFontSize(14.0);
        _contentTextView.delegate = self;
        _contentTextView.returnKeyType = UIReturnKeyDone;
//        _contentTextView.backgroundColor = kColorHEX(0x00fffff);
    }
    return _contentTextView;
}

- (UILabel *)placeholderLabel {
    if (!_placeholderLabel) {
        _placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(5.0), 0, self.contentTextView.width, KScaleWidth(34.0)) font:KFontSize(14.0) textColor:kColorHEX(0x999999)];
        _placeholderLabel.text = @"告诉TA特别的你要做什么特别的事，好文案更容易见面哦～";
        _placeholderLabel.userInteractionEnabled = NO;
    }
    return _placeholderLabel;
}

- (UIScrollView *)imgBgView {
    if (!_imgBgView) {
        _imgBgView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.contentTextView.maxY + KScaleWidth(75.0), self.width, KScaleWidth(85.0))];
    }
    return _imgBgView;
}

- (UIButton *)addImgBtn {
    if (!_addImgBtn) {
        _addImgBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(10.0) frame:CGRectMake(KScaleWidth(16.0), 0, KScaleWidth(90.0), KScaleWidth(78.0)) title:@"添加优质\n首图更吸引人" titleColor:kColorHEX(0x999999)];
        _addImgBtn.titleLabel.numberOfLines = 0;
        _addImgBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        _addImgBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [_addImgBtn setTitleEdgeInsets:UIEdgeInsetsMake(KScaleWidth(10.0), 0, -KScaleWidth(10.0), 0)];
        [_addImgBtn setBackgroundImage:ImageNamed(@"pub_add_img_icon") forState:UIControlStateNormal];
        [_addImgBtn addTarget:self action:@selector(addImgAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addImgBtn;
}

- (UIScrollView *)topicBgView {
    if (!_topicBgView) {
        _topicBgView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.imgBgView.maxY + KScaleWidth(60.0), self.width, KScaleWidth(58.0))];
    }
    return _topicBgView;
}

- (XMTopicView *)topicView {
    if (!_topicView) {
        _topicView = [[XMTopicView alloc] initWithFrame:self.bounds];
    }
    return _topicView;
}

- (UILabel *)addressLabel {
    if (!_addressLabel) {
        _addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(90.0), 0, self.width - KScaleWidth(90.0) - KScaleWidth(30.0), KScaleWidth(58.0)) font:KFontSize(15.0) textColor:kColorHEX(0x000000) textAlignment:NSTextAlignmentRight];
    }
    return _addressLabel;
}

- (UISwitch *)secretSwitch {
    if (!_secretSwitch) {
        _secretSwitch = [[UISwitch alloc] init];
        _secretSwitch.x = self.width - _secretSwitch.width - KScaleWidth(20.0);
        _secretSwitch.y = (KScaleWidth(58.0) - _secretSwitch.height)/2;
    }
    return _secretSwitch;
}

- (NSMutableArray *)selectImgArr {
    if (!_selectImgArr) {
        _selectImgArr = [NSMutableArray array];
    }
    return _selectImgArr;
}

- (NSMutableArray *)selectTopicArr {
    if (!_selectTopicArr) {
        _selectTopicArr = [NSMutableArray array];
    }
    return _selectTopicArr;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
